package cn.tedu.medicalappointmentsystemk.service.impl;

import cn.tedu.medicalappointmentsystemk.dao.persist.repository.ScheduleRepository;
import cn.tedu.medicalappointmentsystemk.handler.ServiceException;
import cn.tedu.medicalappointmentsystemk.pojo.bo.ScheduleSelectAllBO;
import cn.tedu.medicalappointmentsystemk.pojo.bo.ScheduleSelectBO;
import cn.tedu.medicalappointmentsystemk.pojo.param.ScheduleAddParam;
import cn.tedu.medicalappointmentsystemk.pojo.po.PageData;
import cn.tedu.medicalappointmentsystemk.pojo.po.SchedulePO;
import cn.tedu.medicalappointmentsystemk.pojo.vo.ScheduleSelectVO;
import cn.tedu.medicalappointmentsystemk.response.ServiceCode;
import cn.tedu.medicalappointmentsystemk.service.ScheduleService;
import cn.tedu.medicalappointmentsystemk.util.PageInfoToPageDataConverter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleRepository scheduleRepository;

    @Override
    public void insert(ScheduleAddParam param) {
        SchedulePO po = new SchedulePO();
        String dutyShift = scheduleRepository.selectDutyShift(param.getDoctorId(),param.getCreateDate());
        if (dutyShift!=null){
            String message = "当前医生当天已存在排班!";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        BeanUtils.copyProperties(param,po);
        Integer row = scheduleRepository.insert(po);
        if (row != 1){
            String message = "新增排班失败!请稍后再试!";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Value("${medical.dao.default-query-page-size}")
    private int pageSize;

    @Override
    public PageData<ScheduleSelectBO> selectByString(Integer pageNum ,String[] dates) {
        List<Date> dateList = new ArrayList<>();
        LocalDateTime day = LocalDateTime.now();
        Integer year = day.getYear();
        for (String date : dates){
            LocalDateTime localDateTime = LocalDateTime.parse(year+"-"+date+"T00:00:00");
            dateList.add(Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));
        }
        PageHelper.startPage(pageNum,30);
        Page<ScheduleSelectBO> page = scheduleRepository.selectByDateList(dateList);
        List<ScheduleSelectBO> scheduleSelectBOS = page.getResult();
        for (ScheduleSelectBO bo : scheduleSelectBOS){
            List<ScheduleSelectVO> schedule = bo.getSchedule();
            List<ScheduleSelectVO> scheduleSelectVOS = new ArrayList<>();
            ScheduleSelectVO vos = new ScheduleSelectVO(null,new Date(0L),null);
            for (int i = 0; i < 7; i++) {
                boolean add = false;
                for (ScheduleSelectVO vo : schedule) {
                    if (i == vo.getDay()) {
                        scheduleSelectVOS.add(vo);
                        add = true;
                    }
                }
                if (!add) {
                    scheduleSelectVOS.add(vos);
                }
            }
            bo.setSchedule(scheduleSelectVOS);
        }
        PageInfo<ScheduleSelectBO> pageInfo = new PageInfo<>(page);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<ScheduleSelectAllBO> selectAll(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        Page<ScheduleSelectAllBO> page = scheduleRepository.selectAll();
        PageInfo<ScheduleSelectAllBO> pageInfo = new PageInfo<>(page);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public void deleteById(Long id) {
        Integer row=scheduleRepository.deleteById(id);
        if(row!=1){
            String message="删除失败";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
    }

    @Override
    public PageData<ScheduleSelectAllBO> searchLastMonth(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        Page<ScheduleSelectAllBO> page = scheduleRepository.searchLastMonth();
        PageInfo<ScheduleSelectAllBO> pageInfo = new PageInfo<>(page);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<ScheduleSelectAllBO> searchLastWeek(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        Page<ScheduleSelectAllBO> page = scheduleRepository.searchLastWeek();
        PageInfo<ScheduleSelectAllBO> pageInfo = new PageInfo<>(page);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<ScheduleSelectAllBO> searchToday(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        Page<ScheduleSelectAllBO> page = scheduleRepository.searchToday();
        PageInfo<ScheduleSelectAllBO> pageInfo = new PageInfo<>(page);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public List<ScheduleSelectAllBO> selectByNickname(String nickname) {
        return scheduleRepository.selectByNickname(nickname);
    }
}
